package com.swiftride.template;

import cn.hutool.core.util.StrUtil;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.model.ciModel.auditing.ImageAuditingRequest;
import com.qcloud.cos.model.ciModel.auditing.ImageAuditingResponse;
import com.qcloud.cos.region.Region;
import com.swiftride.properties.CosProperties;
import org.springframework.beans.factory.DisposableBean;

import java.io.InputStream;

public class CosTemplate implements DisposableBean {

    private final CosProperties cosProperties;

    private final COSClient cosClient;

    public CosTemplate(CosProperties cosProperties) {
        this.cosProperties = cosProperties;
        // 1 初始化用户身份信息（secretId, secretKey）。
        COSCredentials cred = new BasicCOSCredentials(cosProperties.getSecretId(), cosProperties.getSecretKey());
        // 2 设置 bucket 的地域
        Region region = new Region(cosProperties.getRegion());
        ClientConfig clientConfig = new ClientConfig(region);
        // 从 5.6.54 版本开始，默认使用了 https
        clientConfig.setHttpProtocol(HttpProtocol.https);
        // 3 生成 cos 客户端。
        this.cosClient = new COSClient(cred, clientConfig);
    }

    public String uploadFile(InputStream inputStream, String folder, String fileName) {
        // 指定文件将要存放的存储桶
        String bucketName = cosProperties.getBucketName();
        // 指定文件上传到 COS 上的路径，即对象键。例如对象键为 folder/picture.jpg，则表示将文件 picture.jpg 上传到 folder 路径下
        String key = folder + "/" + fileName;
        ObjectMetadata objectMetadata = new ObjectMetadata();
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, inputStream, objectMetadata);
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
        if (StrUtil.isEmpty(putObjectResult.getETag())) {
            throw new RuntimeException("文件上传失败");
        }
        //@TODO 后续修改为发送图片的情况下进行审核,或者不同文件进行不同审核
        return String.format(cosProperties.getFilePathTemplate(), bucketName, cosProperties.getRegion(), folder, fileName);
    }

    private Boolean fileAuditing(String bucketName, String key) {
        //1.创建任务请求对象
        ImageAuditingRequest request = new ImageAuditingRequest();
        //2.添加请求参数 参数详情请见 API 接口文档
        //2.1设置请求 bucket
        request.setBucketName(bucketName);
        //2.2设置审核类型
        request.setDetectType("porn,ads");
        //2.3设置 bucket 中的图片位置
        request.setObjectKey(key);
        //3.调用接口,获取任务响应对象
        ImageAuditingResponse response = cosClient.imageAuditing(request);

        return "0".equals(response.getResult());
    }


    /**
     * 删除图片的方法
     *
     * @param bucket 存储桶名称
     * @param key    文件路径
     */
    public void delete(String bucket, String key) {
        cosClient.deleteObject(bucket, key);
    }

    /**
     * 销毁方法
     */
    @Override
    public void destroy() {
        if (this.cosClient != null) {
            this.cosClient.shutdown();
        }
    }
}
